<?php
/**
 * 账单项Model
 * @author solar
 */
class BillItemModel extends BaseModel 
{
	public static $name='bill_item';
	public static $primary='bi_id';
	
	public function __construct() {
		parent::__construct(CoreFactory::db(),self::$name,self::$primary);
	}
	
	/**
	 * 批量插入账单项
	 * @author solar
	 * @param int $c_id
	 * @param int $b_id
	 * @param array $itemList
	 * @return int
	 */
	public function batchInsert($c_id,$b_id,$itemList) {
		$aValues=array();
		foreach($itemList as $itemInfo){
			$aValues[]='('.$c_id.','.$b_id.','.$itemInfo['u_id'].','.$itemInfo['bi_pay'].','.$itemInfo['bi_consume'].')';
		}
		$sql='INSERT INTO `bill_item`(`c_id`,`b_id`,`u_id`,`bi_pay`,`bi_consume`) values '.implode(',',$aValues);
		$this->db->query($sql);
		return $this->db->affectedRows();
	}
	
	/**
	 * 统计用户总支出和总消费金额
	 * @author solar
	 * @param int $checkout_id
	 * @return list
	 */
	public function statUser($checkout_id) {
		$sql = 'SELECT u_id AS user_id,SUM(bi_pay) AS pay,SUM(bi_consume) AS consume
		 FROM `bill_item` WHERE c_id='.$checkout_id.' GROUP BY u_id';
		return $this->db->fetchAll($sql);
	}
}

?>